package com.bianpai.core;

import com.bianpai.execution.ExecutionStrategy;
import com.bianpai.execution.ParallelExecutionStrategy;
import com.bianpai.execution.SequentialExecutionStrategy;

/**
 * 流程构建器，用于简化流程的创建
 */
public class FlowBuilder {
    private Flow flow;
    
    private FlowBuilder(String id, String name) {
        this.flow = new Flow(id, name);
    }
    
    /**
     * 创建流程构建器
     * @param id 流程ID
     * @param name 流程名称
     * @return 流程构建器
     */
    public static FlowBuilder create(String id, String name) {
        return new FlowBuilder(id, name);
    }
    
    /**
     * 设置流程描述
     * @param description 描述
     * @return 流程构建器
     */
    public FlowBuilder description(String description) {
        flow.setDescription(description);
        return this;
    }
    
    /**
     * 添加节点
     * @param node 节点
     * @return 流程构建器
     */
    public FlowBuilder addNode(Node node) {
        flow.addNode(node);
        return this;
    }
    
    /**
     * 添加节点依赖关系
     * @param nodeId 当前节点ID
     * @param dependsOnNodeId 依赖的节点ID
     * @return 流程构建器
     */
    public FlowBuilder addDependency(String nodeId, String dependsOnNodeId) {
        flow.addDependency(nodeId, dependsOnNodeId);
        return this;
    }
    
    /**
     * 使用顺序执行策略
     * @return 流程构建器
     */
    public FlowBuilder useSequentialExecution() {
        flow.setExecutionStrategy(new SequentialExecutionStrategy());
        return this;
    }
    
    /**
     * 使用并行执行策略
     * @return 流程构建器
     */
    public FlowBuilder useParallelExecution() {
        flow.setExecutionStrategy(new ParallelExecutionStrategy());
        return this;
    }
    
    /**
     * 使用并行执行策略，指定最大线程数
     * @param maxThreads 最大线程数
     * @return 流程构建器
     */
    public FlowBuilder useParallelExecution(int maxThreads) {
        flow.setExecutionStrategy(new ParallelExecutionStrategy(maxThreads));
        return this;
    }
    
    /**
     * 使用自定义执行策略
     * @param strategy 执行策略
     * @return 流程构建器
     */
    public FlowBuilder useExecutionStrategy(ExecutionStrategy strategy) {
        flow.setExecutionStrategy(strategy);
        return this;
    }
    
    /**
     * 构建流程
     * @return 流程
     */
    public Flow build() {
        return flow;
    }
}